package edu.fourtwelve.util;

import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 *
 * @author cxh5200
 */
public class LoggingSetup {

    private static final Logger log = Logger.getLogger(LoggingSetup.class.getName());

    public static void setupLogging() {
        File loggingConfigurationFile =
                new File("logging.properties");

        /*
         * it only generates the configuration file if it doesn't exist.
         */

        if (!loggingConfigurationFile.exists()) {
            Writer output = null;
            try {
                output = new BufferedWriter(
                        new FileWriter(loggingConfigurationFile));

                // The configuration file is a property file.
                // The Properties class gives support to
                // define and persist the logging configuration.
                Properties logConf = new Properties();
                logConf.setProperty("handlers",
                        //                        "java.util.logging.FileHandler," + 
                        "java.util.logging.ConsoleHandler");

                logConf.setProperty(".level", "INFO");
                logConf.setProperty(
                        "java.util.logging.ConsoleHandler.level",
                        "INFO");
                logConf.setProperty(
                        "java.util.logging.ConsoleHandler.formatter",
                        "java.util.logging.SimpleFormatter");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.level",
//                        "ALL");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.pattern",
//                        "log/application.log");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.limit",
//                        "50000");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.count", "1");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.formatter",
//                        "java.util.logging.XMLFormatter");
                logConf.store(output, "Generated");
            } catch (IOException ex) {
                log.log(Level.WARNING,
                        "Logging configuration file not created",
                        ex);
            } finally {
                try {
                    output.close();
                } catch (IOException ex) {
                    log.log(Level.WARNING,
                            "Problems saving the logging configuration file to disk",
                            ex);
                }
            }
        }
        // This is the way to define the system
        // property without changing the command line.
        // It has the same effect of the parameter
        // -Djava.util.logging.config.file
        Properties prop = System.getProperties();
        prop.setProperty(
                "java.util.logging.config.file",
                "logging.properties");

        // It creates the log directory if it doesn't exist
        // In the configuration file above we specify this
        // folder to store log files:
        // logConf.setProperty(
        //         "java.util.logging.FileHandler.pattern",
        //         "log/application.log");
//        File logDir = new File("log");
//        if (!logDir.exists()) {
//            log.info("Creating the logging directory");
//            logDir.mkdir();
//        }

        // It overwrites the current logging configuration
        // to the one in the configuration file.
        try {
            LogManager.getLogManager().readConfiguration();
        } catch (IOException ex) {
            log.log(Level.WARNING,
                    "Problems loading the logging "
                    + "configuration file", ex);
        }
    }

    public static void setupLogging(boolean Debug) {
        File loggingConfigurationFile =
                new File("logging.properties");


        Writer output = null;
        try {
            output = new BufferedWriter(
                    new FileWriter(loggingConfigurationFile));

            // The configuration file is a property file.
            // The Properties class gives support to
            // define and persist the logging configuration.
            Properties logConf = new Properties();
            logConf.setProperty("handlers",
                    //                        "java.util.logging.FileHandler," + 
                    "java.util.logging.ConsoleHandler");

            if (Debug) {
                logConf.setProperty(".level", "ALL");
            } else {
                logConf.setProperty(".level", "INFO");
            }
            if (Debug) {
                logConf.setProperty(
                        "java.util.logging.ConsoleHandler.level",
                        "FINE");
            } else {
                logConf.setProperty(
                        "java.util.logging.ConsoleHandler.level",
                        "INFO");
            }
            logConf.setProperty(
                    "java.util.logging.ConsoleHandler.formatter",
                    "java.util.logging.SimpleFormatter");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.level",
//                        "ALL");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.pattern",
//                        "log/application.log");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.limit",
//                        "50000");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.count", "1");
//                logConf.setProperty(
//                        "java.util.logging.FileHandler.formatter",
//                        "java.util.logging.XMLFormatter");
            logConf.store(output, "Generated");
        } catch (IOException ex) {
            log.log(Level.WARNING,
                    "Logging configuration file not created",
                    ex);
        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                log.log(Level.WARNING,
                        "Problems saving the logging configuration file to disk",
                        ex);
            }
        }
        // This is the way to define the system
        // property without changing the command line.
        // It has the same effect of the parameter
        // -Djava.util.logging.config.file
        Properties prop = System.getProperties();
        prop.setProperty(
                "java.util.logging.config.file",
                "logging.properties");

        // It creates the log directory if it doesn't exist
        // In the configuration file above we specify this
        // folder to store log files:
        // logConf.setProperty(
        //         "java.util.logging.FileHandler.pattern",
        //         "log/application.log");
//        File logDir = new File("log");
//        if (!logDir.exists()) {
//            log.info("Creating the logging directory");
//            logDir.mkdir();
//        }

        // It overwrites the current logging configuration
        // to the one in the configuration file.
        try {
            LogManager.getLogManager().readConfiguration();
        } catch (IOException ex) {
            log.log(Level.WARNING,
                    "Problems loading the logging "
                    + "configuration file", ex);
        }
    }
}
